/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bean.chart.iterfaces;

import com.bean.chart.Chart;
import com.bean.chart.ChartLabel;
import com.bean.chart.ChartLegend;
import com.bean.chart.ChartUnit;
import com.bean.chart.editors.ChartLegendPosition;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.io.Serializable;
import java.util.List;
import javax.swing.JPanel;

/**
 *
 * @author Lukasz
 */
public abstract class AbstractChartComposite extends JPanel implements AbstractChartCompositeView, 
        AbstractChartTitle, AbstractChartLegend, 
        AbstractChartView, AbstractChartModel, AbstractChartUnit, Serializable{

    public static final String PARAM_NAME_CHART = "chart_visible_property";
    public static final String PARAM_NAME_CHARTLEGEND = "chartLegend_visible_property";
    public static final String PARAM_NAME_CHARTTITLE = "chartTitle_visible_property";
    public static final String PARAM_NAME_UNITVISIBLE = "unit_visible_property";
    public static final String PARAM_NAME_CHARTLEGENDPOSITION = "chartLegend_position_property";
    
    private boolean unitVisible = true;
    
    private ChartLegendPosition chartLegendPosition = ChartLegendPosition.DOWN;
    
    protected ChartLabel chartTitle = new ChartLabel();
    protected AbstractChart chart = new Chart();
    protected ChartLegend chartLegend = new ChartLegend();
    protected ChartUnit chartUnit = new ChartUnit();
    
    
    public AbstractChartComposite(){
        chart.registerObserver(chartLegend);
        chartLegend.setDataProvider(chart);
    }
    
    @Override
    public void setChartVisible(boolean visible) {
        boolean oldVisible = chart.isVisible();
        chart.setVisible(visible);
        propertyChange(PARAM_NAME_CHART, oldVisible, visible);
        
    }

    @Override
    public boolean isChartVisible() {
        return chart.isVisible();
    }

    @Override
    public void setLegendVisible(boolean visible) {
       boolean oldVisible = chartLegend.isVisible();
       chartLegend.setVisible(visible);
       propertyChange(PARAM_NAME_CHARTLEGEND, oldVisible, visible);
    }

    @Override
    public boolean isLegendVisible() {
        return chartLegend.isVisible();
    }

    @Override
    public void setTitleVisible(boolean visible) {
       boolean oldVisible = chartTitle.isVisible();
       chartTitle.setVisible(visible);
       propertyChange(PARAM_NAME_CHARTTITLE, oldVisible, visible);
    }

    @Override
    public boolean isTitleVisible() {
        return chartTitle.isVisible();
    }

    @Override
    public void setUnit(String unit) {
       String oldUnit = chartUnit.getUnit();
       chartUnit.setUnit(unit);
       propertyChange(ChartUnit.PARAM_NAME_UNIT, oldUnit, unit);
    }

    @Override
    public String getUnit() {
        return chartUnit.getUnit();
    }
    
    @Override
     public void setUnitColor(Color color){
         Color oldColor = chartUnit.getUnitColor();
         chartUnit.setUnitColor(color);
         propertyChange(ChartUnit.PARAM_NAME_UNITCOLOR, oldColor, color);
     }
    @Override
    public Color getUnitColor(){
        return chartUnit.getUnitColor();
    }
    
    @Override
    public void setUnitBackground(Color color){
         Color oldColor = chartUnit.getUnitBackground();
         chartUnit.setUnitBackground(color);
         propertyChange(ChartUnit.PARAM_NAME_UNITBCKGCOLOR, oldColor, color);
    }
    @Override
    public Color getUnitBackground(){
        return chartUnit.getUnitBackground();
    }
    
    @Override
    public void setLegendPosition(ChartLegendPosition position){
        ChartLegendPosition oldPosition = this.chartLegendPosition;
        this.chartLegendPosition = position;
        addChartLegend();
        propertyChange(PARAM_NAME_CHARTLEGENDPOSITION, oldPosition, position);
        repaint();
    }
            
    @Override
    public ChartLegendPosition getLegendPosition(){
        return chartLegendPosition;
    }
    
    @Override
    public void setTitleText(String text){
        String oldTitle = chartTitle.getTitleText();
        chartTitle.setTitleText(text);
        propertyChange(ChartLabel.PROP_LABEL, oldTitle, text);
    }    
    @Override
    public String getTitleText(){
        return chartTitle.getTitleText();
    }
    @Override
    public void setTitleBackground(Color color){
        Color oldColor = chartTitle.getTitleBackground();
        chartTitle.setTitleBackground(color);
        propertyChange(ChartLabel.PROP_BACKGROUND, oldColor, color);
    }
    @Override
    public Color getTitleBackground(){
        return chartTitle.getBackground();
    }
    @Override
    public void setTitleForeground(Color color){
        Color oldColor = chartTitle.getTitleForeground();
        chartTitle.setTitleForeground(color);
        propertyChange(ChartLabel.PROP_FOREGROUND, oldColor, color);
    }
    @Override
    public Color getTitleForeground(){
        return chartTitle.getForeground();
    }
    @Override
     public void setLegendLabel(String label){
         String oldLabel = chartLegend.getLegendLabel();
         chartLegend.setLegendLabel(label);
         chartLegend.update();
         propertyChange(ChartLegend.PROP_LABEL, oldLabel, label);
     }
    @Override
     public String getLegendLabel(){
        return chartLegend.getLegendLabel();
     }
        
    @Override
     public void setLegendBackground(Color color){
         Color oldColor = chartLegend.getLegendBackground();
         chartLegend.setLegendBackground(color);
         chartLegend.update();
         propertyChange(ChartLegend.PROP_BACKGROUND, oldColor, color);
     }
    @Override
     public Color getLegendBackground(){
         return chartLegend.getLegendBackground();
     }
    @Override
     public void setLegendForeground(Color color){
         Color oldColor = chartLegend.getLegendForeground();
         chartLegend.setLegendForeground(color);
         chartLegend.update();
         propertyChange(ChartLegend.PROP_FOREGROUND, oldColor, color);
     }
    @Override
     public Color getLegendForeground(){
         return chartLegend.getLegendForeground();
     }
        
    @Override
     public void setLegendLabelVisible(boolean visible){
         boolean oldVisible = chartLegend.isLegendLabelVisible();
         chartLegend.setLegendLabelVisible(visible);
         chartLegend.update();
         propertyChange(ChartLegend.PROP_LABEL_VISIBLE, oldVisible, visible);
     }
    @Override
     public boolean isLegendLabelVisible(){
         return chartLegend.isLegendLabelVisible();
     }
    @Override
     public void setColorSampleVisible(boolean visible){
         boolean oldVisible = chartLegend.isColorSampleVisible();
         chartLegend.setColorSampleVisible(visible);
         chartLegend.update();
         propertyChange(ChartLegend.PROP_SAMPLE_VISIBLE, oldVisible, visible);
     }
    @Override
     public boolean isColorSampleVisible(){
         return chartLegend.isColorSampleVisible();
     }
        
    @Override
     public void setRowCount(int count){
         int oldCount = chartLegend.getRowCount();
         chartLegend.setRowCount(count);
         chartLegend.update();
         propertyChange(ChartLegend.PROP_ROWCOUNT, oldCount, count);
     }
    @Override
     public int getRowCount(){
         return chartLegend.getRowCount();
     }
    @Override
     public void setColumnCount(int count){
         int oldCount = chartLegend.getColumnCount();
         chartLegend.setColumnCount(count);
         chartLegend.update();
         propertyChange(ChartLegend.PROP_COLUMNCOUNT, oldCount, count);
     }
    @Override
     public int getColumnCount(){
         return chartLegend.getColumnCount();
     }
        
    @Override
     public void setColorSampleSize(Dimension d){
         Dimension oldD = chartLegend.getColorSampleSize();
         chartLegend.setColorSampleSize(d);
         propertyChange(ChartLegend.PROP_COLUMNCOUNT, oldD, d);
     }
    @Override
     public Dimension getColorSampleSize(){
         return chartLegend.getColorSampleSize();
     }
        
    @Override
     public void setDataProvider(LegendDataProvider provider){
         chartLegend.setDataProvider(provider);
     }
    @Override
     public LegendDataProvider getDataProvider(){
         return chartLegend.getDataProvider();
     }
    
    @Override
    public void setLineVisible(boolean visible){
        boolean oldVisible = chart.isLineVisible();
        chart.setLineVisible(visible);
        propertyChange(AbstractChart.PARAM_NAME_LINEVISIBLE, oldVisible, visible);
    }
    @Override
    public boolean isLineVisible(){
        return chart.isLineVisible();
    }
    
    @Override
    public void setDotVisible(boolean visible){
        boolean oldVisible = chart.isDotVisible();
        chart.setDotVisible(visible);
        propertyChange(AbstractChart.PARAM_NAME_DOTVISIBLE, oldVisible, visible);
    }
    @Override
    public boolean isDotVisible(){
        return chart.isDotVisible();
    }
    
    @Override
    public void setGridVisible(boolean visible){
        boolean oldVisible = chart.isGridVisible();
        chart.setGridVisible(visible);
        propertyChange(AbstractChart.PARAM_NAME_GRIDVISIBLE, oldVisible, visible);
    }
    @Override
    public boolean isGridVisible(){
        return chart.isGridVisible();
    }
    
    @Override
    public void setValueVisible(boolean  visible){
        boolean oldVisible = chart.isValueVisible();
        chart.setValueVisible(visible);
        propertyChange(AbstractChart.PARAM_NAME_VALUEVISIBLE, oldVisible, visible);
    }
    @Override
    public boolean isValueVisible(){
        return chart.isValueVisible();
    }

    @Override
    public void setChartColor(String name,Color color){
        chart.setChartColor(name, color);
    }

    @Override
    public Color getChartColor(String name){
        return chart.getChartColor(name);
    }
    
    @Override
    public void setChartColors(Color[] color){
        chart.setChartColors(color);
    }

    @Override
    public Color[] getChartColors(){
        return chart.getChartColors();
    }

    @Override
    public void setChartColors(int i , Color color){
        chart.setChartColors(i, color);
    }

    @Override
    public Color getChartColors(int i){
        return chart.getChartColors(i);
    }
    
    @Override
    public void setBackgroundColor(Color color){
        Color oldColor = chart.getBackgroundColor();
        chart.setBackgroundColor(color);
        propertyChange(AbstractChart.PARAM_NAME_BCKGCOLOR, oldColor, color);
    }
    @Override
    public Color getBackgroundColor(){
        return chart.getBackgroundColor();
    }
    
    @Override
    public void setValueColor(Color color){
        Color oldColor = chart.getValueColor();
        chart.setValueColor(color);
        propertyChange(AbstractChart.PARAM_NAME_VALUECOLOR, oldColor, color);
    }
    @Override
    public Color getValueColor(){
        return chart.getValueColor();
    }
    
    @Override
    public void setGridColor(Color color){
        Color oldColor = chart.getGridColor();
        chart.setGridColor(color);
        propertyChange(AbstractChart.PARAM_NAME_GRIDCOLOR, oldColor, color);
    }
    @Override
    public Color getGridColor(){
        return chart.getGridColor();
    }
    
    @Override
    public void setValuesCount(int count){
        int oldCount = chart.getValuesCount();
        chart.setValuesCount(count);
        propertyChange(AbstractChart.PARAM_NAME_COUNTVALUE, oldCount, count);
    }
    @Override
    public int getValuesCount(){
        return chart.getValuesCount();
    }
    
    @Override
    public void setDotSize(int size){
        int oldSize = chart.getDotSize();
        chart.setDotSize(size);
        propertyChange(AbstractChart.PARAM_NAME_DOTSIZE, oldSize, size);
    }
    @Override
    public int getDotSize(){
        return chart.getDotSize();
    }
    
    @Override
    public void setMarksCount(int count){
        int oldCount = chart.getMarksCount();
        chart.setMarksCount(count);
        propertyChange(AbstractChart.PARAM_NAME_STEPMARKSCOUNT, oldCount, count);
    }
    @Override
    public int getMarksCount(){
        return chart.getMarksCount();
    }
    
    @Override
    public void setStrokeSize(int size){
        int oldSize = chart.getStrokeSize();
        chart.setStrokeSize(size);
        propertyChange(AbstractChart.PARAM_NAME_STROKESIZE, oldSize, size);
    }
    @Override
    public int getStrokeSize(){
        return chart.getStrokeSize();
    }
    
    @Override
    public void addChartData(String chartName){
        chart.addChartData(chartName);
    }
    @Override
    public void removeChartData(String chartName){
        chart.removeChartData(chartName);
    }
    @Override
    public void removeAllCharts(){
        chart.removeAllCharts();;
    }
    @Override
    public List<Double> getChartData(String chartName){
        return chart.getChartData(chartName);
    }
    @Override
    public void addChartDataPoint(String chartName,Double value){
        chart.addChartDataPoint(chartName, value);
    }
    @Override
    public void setMaxYValue(double maxYValue){
        double oldMax = chart.getMaxYValue();
        chart.setMaxYValue(maxYValue);
        propertyChange(AbstractChart.PARAM_NAME_MAXYVALUE, oldMax, maxYValue);
    }
    @Override
    public double getMaxYValue(){
        return chart.getMaxYValue();
    }
    @Override
    public void setRunning(boolean running){
        boolean oldRunning = chart.isRunning();
        chart.setRunning(running);
        propertyChange(AbstractChart.PARAM_NAME_RUNNING, oldRunning, running);
    }
    @Override
    public boolean isRunning(){
        return chart.isRunning();
    }
    @Override
    public void setRefreshRate(long milisec){
        long oldMilisec = chart.getRefreshRate();
        chart.setRefreshRate(milisec);
        propertyChange(AbstractChart.PARAM_NAME_REFRESHRATE, oldMilisec, milisec);
    }
    @Override
    public long getRefreshRate(){
        return chart.getRefreshRate();
    }
    @Override
    public void setReadMode(ChartReadMode mode){
        ChartReadMode oldMode = chart.getReadMode();
        chart.setReadMode(mode);
        propertyChange(AbstractChart.PARAM_NAME_READMODE, oldMode, mode);
    }
    @Override
    public ChartReadMode getReadMode(){
        return chart.getReadMode();
    }
    
        @Override
    public void setXVisible(boolean visible){
        boolean oldVisible = chart.isXVisible();
        chart.setXVisible(visible);
        propertyChange(AbstractChart.PARAM_NAME_XVISIBLE, oldVisible, visible);
    }
    @Override
    public boolean isXVisible(){
        return chart.isXVisible();
    }
    
    @Override
    public void setYVisible(boolean visible){
        boolean oldVisible = chart.isYVisible();
        chart.setYVisible(visible);
        propertyChange(AbstractChart.PARAM_NAME_YVISIBLE, oldVisible, visible);
    }
    @Override
    public boolean isYVisible(){
        return chart.isYVisible();
    }
    
    protected abstract void addChartLegend();
    protected abstract void propertyChange(String property,boolean oldval, boolean newVal);
    protected abstract void propertyChange(String property,String oldval, String newVal);
    protected abstract void propertyChange(String property,Object oldval, Object newVal);
}
